import { Component, OnInit, ViewChild } from "@angular/core";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { Router } from "@angular/router";
import { NzMessageService } from "ng-zorro-antd";
import { SimpleTree } from "src/app/shared/type/tree";
import { environment } from "src/environments/environment";
import { LoginService } from "./login.service";
@Component({
  selector: "n-login",
  templateUrl: "./login.component.html",
  styleUrls: ["./login.component.scss"],
})
export class LoginComponent implements OnInit {
  validateForm = this.fb.group({
    userName: [null, [Validators.required]],
    password: [null, [Validators.required]],
    remember: [true],
  });
  menuTable: any[];

  constructor(
    private fb: FormBuilder,
    private msg: NzMessageService,
    private loginService: LoginService,
    private router: Router
  ) {}

  ngOnInit() {
    sessionStorage.clear();
  }

  submitForm() {
    this.loginService
      .login({
        username: this.validateForm.value.userName,
        password: this.validateForm.value.password,
      })
      .subscribe((response) => {
        if (response.code === 0) {
          const loginInfo = Object.assign({...response.data});
          delete loginInfo.modules;
          sessionStorage.setItem("loginInfo", JSON.stringify(loginInfo));
          sessionStorage.setItem("loginName", this.validateForm.value.userName);
          sessionStorage.setItem("login", "1");
          sessionStorage.setItem("appIsLogin", "1");
          sessionStorage.setItem("id", response.data.id);
          sessionStorage.setItem("name", response.data.name);
          const authority = [];
          response.data.modules.forEach((item: any) => {
            item.menu.forEach((key) => {
              if (key.children) {
                key.children.forEach((value) => {
                  authority.push({ list: value.menuList, url: value.url });
                });
              }
            });
          });
          sessionStorage.setItem("authority", JSON.stringify(authority));
          // 本地开发环境
          if (environment.production) {
            response.data.modules.forEach((item) => {
              SimpleTree.updatePropertyValue(
                (item.menu as unknown) as SimpleTree[],
                {
                  url: (url: string) => {
                    return url
                      ? url.replace(environment.replaceField, environment.htmlUrl)
                      : url;
                  },
                }
              );
            });
          }
          sessionStorage.setItem("menu", JSON.stringify(response.data));

          // 待app老后台删除后， 这段可删除
          const menuList = response.data.modules.filter(item => item.id === 99);
          if (menuList.length > 0) {
            sessionStorage.setItem("menuList", JSON.stringify(menuList[0].menu));
          }
          // delete end

          this.router.navigate(["/"]);
        } else {
          this.msg.error(response.msg);
        }
      });
  }
}
